package task

import (
	"bufio"
	"fmt"
	"higame/modules/fs"
	"higame/modules/utils"
	"io"
	"io/ioutil"
	"os/exec"
	"path"
	"runtime"
)

var mgoFileName = "mongodb-linux-x86_64-rhel70-4.2.0"
var mgoUrl = "http://downloads.mongodb.org/linux/" + mgoFileName + ".tgz"

func Deploy() {
	platform := runtime.GOOS
	if platform == "windows" {
		fmt.Println("暂时不支持这个平台的环境部署")
	} else if platform == "linux" {
		fmt.Println("*****************************【下载mongodb】*************************************")
		execCommand("wget", mgoUrl)
		fmt.Println("*****************************【安装mongodb】*************************************")
		execCommand("tar", "-xzvf", utils.JoinString("./%v.tgz", mgoFileName))
		execCommand("mkdir", "-p", "/usr/local/server")
		execCommand("mv", "./"+mgoFileName, "/usr/local/server/mongodb/")

		remove("/usr/local/bin/mongo")
		remove("/usr/local/bin/mongod")
		remove("/usr/local/bin/mongodump")
		remove("/usr/local/bin/mongoexport")
		remove("/usr/local/bin/mongofiles")
		remove("/usr/local/bin/mongoimport")
		remove("/usr/local/bin/mongoreplay")
		remove("/usr/local/bin/mongorestore")
		remove("/usr/local/bin/mongos")
		remove("/usr/local/bin/mongostat")
		remove("/usr/local/bin/mongotop")

		execCommand("ln", "-s", "/usr/local/server/mongodb/bin/mongo", "/usr/local/bin/mongo")
		execCommand("ln", "-s", "/usr/local/server/mongodb/bin/mongod", "/usr/local/bin/mongod")
		execCommand("ln", "-s", "/usr/local/server/mongodb/bin/mongodump", "/usr/local/bin/mongodump")
		execCommand("ln", "-s", "/usr/local/server/mongodb/bin/mongoexport", "/usr/local/bin/mongoexport")
		execCommand("ln", "-s", "/usr/local/server/mongodb/bin/mongofiles", "/usr/local/bin/mongofiles")
		execCommand("ln", "-s", "/usr/local/server/mongodb/bin/mongoimport", "/usr/local/bin/mongoimport")
		execCommand("ln", "-s", "/usr/local/server/mongodb/bin/mongoreplay", "/usr/local/bin/mongoreplay")
		execCommand("ln", "-s", "/usr/local/server/mongodb/bin/mongorestore", "/usr/local/bin/mongorestore")
		execCommand("ln", "-s", "/usr/local/server/mongodb/bin/mongos", "/usr/local/bin/mongos")
		execCommand("ln", "-s", "/usr/local/server/mongodb/bin/mongostat", "/usr/local/bin/mongostat")
		execCommand("ln", "-s", "/usr/local/server/mongodb/bin/mongotop", "/usr/local/bin/mongotop")

		execCommand("mkdir", "-p", "/data/db")
		execCommand("/usr/local/server/mongodb/bin/mongod", "--port=27017", "--dbpath=/data/db", "--logpath=/var/log/mongodb.log", "--fork")
		execCommand("chmod +x /etc/rc.local")
		execCommand("echo", "mongod --port=27017 --dbpath=/data/db --logpath=/var/log/mongodb.log --fork", ">>", "/etc/rc.local")

		//fmt.Println("*****************************【开启安装nginx】*************************************")
		//execCommand("yum", "install", "-y", "nginx")
		//execCommand("chkconfig", "nginx", "on")
		fmt.Println("*****************************【写入service文件】*************************************")
		serviceFile()
		fmt.Println(`启动		service higame start
停止		service higame stop
查看状态		service higame status
开机启动		service higame enable`)
	} else {
		fmt.Println("暂时不支持这个平台的环境部署")
	}

}
func execCommand(name string, arg ...string) {
	//函数返回一个*Cmd，用于使用给出的参数执行name指定的程序
	cmd := exec.Command(name, arg...)

	//显示运行的命令
	//StdoutPipe方法返回一个在命令Start后与命令标准输出关联的管道。Wait方法获知命令结束后会关闭这个管道，一般不需要显式的关闭该管道。
	stdout, err := cmd.StdoutPipe()

	if err != nil {
		fmt.Println(err)
	}

	cmd.Start()
	//创建一个流来读取管道内内容，这里逻辑是通过一行一行的读取的
	reader := bufio.NewReader(stdout)

	//实时循环读取输出流中的一行内容
	for {
		line, err2 := reader.ReadString('\n')
		if err2 != nil || io.EOF == err2 {
			break
		}
		fmt.Println(line)
	}

	//阻塞直到该命令执行完成，该命令必须是被Start方法开始执行的
	cmd.Wait()
}

func remove(path string) {
	if fs.PathExists(path) {
		execCommand("rm", "-rf", path)
	}
}

func serviceFile() {
	text := `[Unit]
Description=higame

[Service]
PIDFile=/tmp/higame.pid-2000
User=root
Group=root
RestartSec=5s
ExecStart=%v
WorkingDirectory=%v

[Install]
WantedBy=multi-user.target`
	systemdPath := "/lib/systemd/system/higame.service"
	fullPath := path.Join(utils.RootPath(), utils.GetAppName())
	content := []byte(utils.JoinString(text, fullPath, utils.RootPath()))
	err := ioutil.WriteFile(systemdPath, content, 0777)
	if err != nil {
		fmt.Println(err)
	}
}
